package com.lckj.android.shell.module.web;

import android.device.ScanDevice;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.example.lc_print_sdk.PrintUtil;

import org.json.JSONObject;

/**
 * @author lc. 2023-10-22 15:34
 */
public class WebJsInterface {

    private String mPrintCallbackMethod;
    private String mPrintOnVersionCallbackMethod;

    private ScanDevice mScanDevice;
    private final PrintUtil mPrintUtil;
    private PrintUtil.PrinterBinderListener mPrintListener;
    private final WebActivity mHostAct;

    public WebJsInterface(WebActivity activity) {
        mHostAct = activity;
        mPrintUtil = PrintUtil.getInstance(activity);
        mPrintListener = new PrintUtil.PrinterBinderListener() {
            @Override
            public void onPrintCallback(int state) {
                if (!TextUtils.isEmpty(mPrintCallbackMethod)) {
                    mHostAct.callJsMethod(mPrintCallbackMethod + "(" + state + ")");
                }
            }

            @Override
            public void onVersion(String version) {
                if (!TextUtils.isEmpty(mPrintOnVersionCallbackMethod)) {
                    mHostAct.callJsMethod(mPrintOnVersionCallbackMethod + "('" + version + "')");
                }
            }
        };
    }

    private synchronized void setPrintListener() {
        if (mPrintListener == null) {
            mPrintListener = new PrintUtil.PrinterBinderListener() {
                @Override
                public void onPrintCallback(int state) {
                    if (!TextUtils.isEmpty(mPrintCallbackMethod)) {
                        mHostAct.callJsMethod(mPrintCallbackMethod + "(" + state + ")");
                    }
                }

                @Override
                public void onVersion(String version) {
                    if (!TextUtils.isEmpty(mPrintOnVersionCallbackMethod)) {
                        mHostAct.callJsMethod(mPrintOnVersionCallbackMethod + "('" + version + "')");
                    }
                }
            };
            PrintUtil.setPrintEventListener(mPrintListener);
        }
    }

    private synchronized ScanDevice getScanDevice() {
        if (mScanDevice == null) {
            mScanDevice = new ScanDevice();
        }
        return mScanDevice;
    }

    @JavascriptInterface
    public void setPrintEventListener(String param) {
        try {
            JSONObject json = new JSONObject(param);
            mPrintCallbackMethod = json.optString("onPrintCallback", "");
            mPrintOnVersionCallbackMethod = json.optString("onVersion", "");
        } catch (Exception e) {
            // no-op
        }
        setPrintListener();
    }

    /**
     * 设置回纸距离
     *
     * @param returnDistance 回纸距离
     */
    @JavascriptInterface
    public void setUnwindPaperLen(int returnDistance) {
        PrintUtil.setUnwindPaperLen(returnDistance);
    }

    /**
     * 设置黑标检测
     *
     * @param enableMark true:开 、false:关
     */
    @JavascriptInterface
    public void printEnableMark(boolean enableMark) {
        PrintUtil.printEnableMark(enableMark);
    }

    /**
     * 设置浓度
     *
     * @param density 浓度值【1~39】
     */
    @JavascriptInterface
    public void printConcentration(int density) {
        PrintUtil.printConcentration(density);
    }

    /**
     * 添加打印文字
     *
     * @param text 文本内容
     */
    @JavascriptInterface
    public void printText(String text) {
        PrintUtil.printText(text);
    }

    /**
     * 开始走纸
     *
     * @param distance 走纸距离
     */
    @JavascriptInterface
    public void printGoToNextMark(int distance) {
        mPrintUtil.printGoToNextMark(distance);
    }

    ///////////////////////////////////////////////////////////////////////////
    // scan interface
    ///////////////////////////////////////////////////////////////////////////

    @JavascriptInterface
    public boolean isScanOpened() {
        return getScanDevice().isScanOpened();
    }

    @JavascriptInterface
    public boolean openScan() {
        return getScanDevice().openScan();
    }

    @JavascriptInterface
    public boolean closeScan() {
        return getScanDevice().closeScan();
    }

    @JavascriptInterface
    public boolean startScan() {
        return getScanDevice().startScan();
    }

    @JavascriptInterface
    public boolean stopScan() {
        return getScanDevice().stopScan();
    }

    /**
     * @param mode mode:4 开 启连续扫描 mode:8 关 闭连续扫描
     */
    @JavascriptInterface
    public void setScanLaserMode(int mode) {
        getScanDevice().setScanLaserMode(mode);
    }

    /**
     * @param mode mode:0 广播模式 mode:1 编辑框模式 mode:2 键盘模式
     */
    @JavascriptInterface
    public void setOutScanMode(int mode) {
        getScanDevice().setOutScanMode(mode);
    }

    /**
     * @return mode mode:0 广播模式 mode:1 编辑框模式 mode:2 键盘模式
     */
    @JavascriptInterface
    public int getScanLaserMode() {
        return getScanDevice().getScanLaserMode();
    }

    /**
     * @return mode mode:0 广播模式 mode:1 编辑框模式 mode:2 键盘模式
     */
    @JavascriptInterface
    public int getOutScanMode() {
        return getScanDevice().getOutScanMode();
    }

    @JavascriptInterface
    public boolean resetScan() {
        return getScanDevice().resetScan();
    }

    @JavascriptInterface
    public String getScanCodeValue() {
        return getScanDevice().getScanCodeValue();
    }

    @JavascriptInterface
    public boolean setScanUnBeep() {
        return getScanDevice().setScanUnBeep();
    }

    @JavascriptInterface
    public boolean setScanBeep() {
        return getScanDevice().setScanBeep();
    }

    @JavascriptInterface
    public boolean getScanBeepState() {
        return getScanDevice().getScanBeepState();
    }

    @JavascriptInterface
    public boolean setScanVibrate() {
        return getScanDevice().setScanVibrate();
    }

    @JavascriptInterface
    public boolean setScanUnVibrate() {
        return getScanDevice().setScanUnVibrate();
    }

    @JavascriptInterface
    public boolean getScanVibrateState() {
        return getScanDevice().getScanVibrateState();
    }

}
